#!/usr/bin/env python
# coding: utf-8

# # 作业5.5

# In[1]:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


# 导入数据

# In[2]:


DScoreBoy = pd.read_excel('./体测分数_男生.xls',
              sheet_name= 0,
              header=0
              )
DScoreGirl = pd.read_excel('./体测分数_女生.xls',
              sheet_name= 0,
              header=0
              )
display(DScoreBoy,DScoreGirl)


# ## 1、对男1000米跑、男引体进行等宽分箱操作，分成3份，并使用饼图绘制百分比

# 对男1000米跑、男引体进行等宽分箱操作

# In[3]:


DLevelBoy=DScoreBoy[['班级','性别','男1000米跑分数','男引体分数']]
DLevelBoy['男1000米跑等级'] = pd.cut(DLevelBoy['男1000米跑分数'],
                                bins=3,
                                labels= ['C','B','A'])
DLevelBoy['男引体等级'] = pd.cut(DLevelBoy['男引体分数'],
                                bins=3,
                                labels= ['C','B','A'])
DLevelBoy


# In[4]:


blevel_1000 = DLevelBoy.groupby(by=['男1000米跑等级']).count()
blevel_pull = DLevelBoy.groupby(by=['男引体等级']).count()


# In[133]:


labels = ['C','B','A']
percent = [0,0,0]
for i in range(0,len(blevel_1000.index)):
    percent[i] = blevel_1000.iloc[i,0]

explode = (0.3,0,0)

fig=plt.figure(figsize=(4,4), dpi=100)
plt.pie(x=percent, # 数据
        explode=explode, # 偏移中⼼量
        labels=labels, # 显示标签
        autopct='%0.1f%%',# 显示百分⽐
        shadow=True # 阴影， 3D效果
       )
plt.title('男1000米跑等级分布',rotation = 0,horizontalalignment = 'right',fontstyle = 'normal',fontsize = 20)
plt.rcParams['font.sans-serif'] = 'FZYaoTi'
plt.savefig("./男1000米跑等级分布-饼图.jpg")


# In[134]:


labels = ['C','B','A']
percent = [0,0,0]
for i in range(0,len(blevel_pull.index)):
    percent[i] = blevel_pull.iloc[i,0]

explode = (0.2,0,0)

fig=plt.figure(figsize=(4,4), dpi=100)
plt.pie(x=percent, # 数据
        explode=explode, # 偏移中⼼量
        labels=labels, # 显示标签
        autopct='%0.1f%%',# 显示百分⽐
        shadow=True # 阴影， 3D效果
       )
plt.title('男引体等级',rotation = 0,horizontalalignment = 'right',fontstyle = 'normal',fontsize = 20)
plt.rcParams['font.sans-serif'] = 'FZYaoTi'
plt.savefig("./男引体等级-饼图.jpg")


# ## 2、对女800米跑、女跳远进行直方图绘制统计各分数段人数，分成4份

# In[135]:


DLevelGirl=DScoreGirl[['班级','性别','女800米跑分数','女跳远分数']]
fig=plt.figure(figsize=(4,4), dpi=100)
x = DLevelGirl['女800米跑分数']
plt.hist(x,bins=4) # 直⽅图
plt.xlabel('分数段',
          fontsize=20,
          rotation =0,
          horizontalalignment='center'
          )
plt.ylabel('累计人数',
          fontsize=20,
          rotation =0,
          horizontalalignment='right'
          )
plt.grid(linestyle = '-.',# 样式
         color = 'green',# 颜⾊
         alpha = 0.75,
         axis = 'y'
        )
plt.title('女800米跑分数分布',
          rotation = 0,
          horizontalalignment = 'center',
          fontstyle = 'normal',
          fontsize = 20)
plt.rcParams['font.sans-serif'] = 'FZYaoTi'
plt.savefig("./女800米跑分数分布-直方图.jpg")


# In[136]:


fig=plt.figure(figsize=(4,4), dpi=100)
plt.hist(x= DLevelGirl['女跳远分数'],
         bins=4,
         label='abcd'
        ) # 直⽅图
plt.xlabel('分数段',
          fontsize=20,
          rotation =0,
          horizontalalignment='center'
          )
plt.ylabel('累计人数',
          fontsize=20,
          rotation =0,
          horizontalalignment='right'
          )
plt.grid(linestyle = '-.',# 样式
         color = 'green',# 颜⾊
         alpha = 0.75,
         axis = 'y'
        )
plt.title('女跳远分数分布',
          rotation = 0,
          horizontalalignment = 'center',
          fontstyle = 'normal',
          fontsize = 20
         )
plt.rcParams['font.sans-serif'] = 'FZYaoTi'
plt.savefig("./女跳远分数分布-直方图.jpg")


# ## 3、使用嵌套饼图对比男女生体重指数进行比例统计，分为正常、低体重、超重、肥胖

# In[99]:


BMIBoy = pd.DataFrame(columns=['班级','BMI'])
BMItemp = pd.DataFrame(data={'班级':[0],'BMI':[0]})

for i in range(0,len(DScoreBoy.index)):
    if DScoreBoy.loc[i,'BMI'] != 0:

        BMItemp['班级']=DScoreBoy.loc[i,'班级']
        BMItemp['BMI']=round(DScoreBoy.loc[i,'BMI'],2)

        BMIBoy= pd.concat([BMIBoy,BMItemp],axis= 0,ignore_index=True)
        
for i in range(0,len(BMIBoy.index)):
    if BMIBoy.loc[i,'BMI']<= 16.4:
        BMIBoy.loc[i,'BMI等级'] = '低体重'
    elif BMIBoy.loc[i,'BMI']<= 23.2:
        BMIBoy.loc[i,'BMI等级'] = '正常'
    elif BMIBoy.loc[i,'BMI']<= 26.3:
        BMIBoy.loc[i,'BMI等级'] = '超重'
    else:
        BMIBoy.loc[i,'BMI等级'] = '肥胖'

BMIGirl = pd.DataFrame(columns=['班级','BMI'])
BMItemp = pd.DataFrame(data={'班级':[0],'BMI':[0]})

for i in range(0,len(DScoreGirl.index)):
    if DScoreGirl.loc[i,'BMI'] != 0:

        BMItemp['班级']=DScoreGirl.loc[i,'班级']
        BMItemp['BMI']=round(DScoreGirl.loc[i,'BMI'],2)

        BMIGirl= pd.concat([BMIGirl,BMItemp],axis= 0,ignore_index=True)
        
for i in range(0,len(BMIGirl.index)):
    if BMIGirl.loc[i,'BMI']<= 16.4:
        BMIGirl.loc[i,'BMI等级'] = '低体重'
    elif BMIGirl.loc[i,'BMI']<= 22.7:
        BMIGirl.loc[i,'BMI等级'] = '正常'
    elif BMIGirl.loc[i,'BMI']<= 25.2:
        BMIGirl.loc[i,'BMI等级'] = '超重'
    else:
        BMIGirl.loc[i,'BMI等级'] = '肥胖'


# In[100]:


dfb = BMIBoy.groupby(by= 'BMI等级').count()
dfg = BMIGirl.groupby(by= 'BMI等级').count()


# In[142]:


fig=plt.figure(figsize=(5,5),dpi=100)#数据集， p1, p2分别对应外部、内部百分⽐例

labelsboy = ['男低体重','男正常','男超重','男肥胖']
labelsgirl = ['女低体重','女正常','女超重','女肥胖']

g=plt.pie(x = dfg['BMI'],
          autopct= '%0.1f%%' ,
          radius=1.2,
          pctdistance=0.9,
          wedgeprops=dict(linewidth=1.5,width=0.3,edgecolor='w'),
          labels=labelgirl,
          labeldistance = 1.02,
          startangle=5
         )

b=plt.pie(x = dfb['BMI'],
          autopct='%0.1f%%',          
          radius=0.6, # 半径
          pctdistance=0.85, # 百分⽐位置
          wedgeprops=dict(linewidth=1.5,width=0.3,edgecolor='w'),# 饼图格式：间隔线宽、饼图宽度、边界颜⾊
          labels=labelsboy,
          labeldistance = 1.1,
          startangle=0
         )

plt.legend(loc = 'right',bbox_to_anchor=(1.2,0.83))
plt.savefig("./男女BMI分布比例-嵌套饼图.jpg")


# In[ ]:




